#define _CRT_SECURE_NO_WARNINGS  1
#pragma warning(disable:6031)
//https://leetcode.cn/contest/weekly-contest-398/problems/sum-of-digit-differences-of-all-pairs/


class Solution {
public:
    int Count(int num)
    {
        int ret = 0;
        do
        {
            ++ret;
            num /= 10;
        } while (num != 0);

        return ret;
    }

    long long sumDigitDifferences(vector<int>& nums)
    {
        int n = nums.size();
        int len = Count(nums[0]);
        long long ret = 0;
        // vector<vector<char>> table(len, vector<char> (n));
        vector<vector<int>> table(len, vector<int>(10));
        for (int i = 0; i < n; i++)
        {
            int tmp = nums[i], index = 0;
            while (tmp != 0)
            {
                int num = tmp % 10;
                ++table[index++][num];
                tmp /= 10;
            }
        }

        // for (int i = 0; i<len; i++)
        // {
        //     for (int j = 0; j<n; j++)
        //         cout << table[i][j];
        //     cout << endl;
        // }

        for (int i = 0; i < len; i++)
            for (int j = 0; j < 9; j++)
                for (int k = j + 1; k < 10; k++)
                    ret += table[i][j] * table[i][k];

        return ret;
    }
};